﻿using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.Network;
using JiveMessenger.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Windows.ApplicationModel;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Text;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Media;

namespace JiveMessenger.Models
{
    [DataContract]
    public enum MessageModelType
    {
        [EnumMember(Value = "in")]
        Incoming,
        [EnumMember(Value = "out")]
        Outgoing,
        [EnumMember(Value = "sysmsg")]
        SystemMessage
    }
    [DataContract]
    public class MessageModel : INotifyPropertyChanged
    {
        private bool _isDeserializing = false;
        public bool IsLastInBatch { get; set; }

        [DataMember(Order = 0)]
        public string SenderID { get; set; }

        private MessageModelType _type;
        [DataMember(Order = 2)]
        public MessageModelType Type
        {
            get { return _type; }
            set
            {
                if (_type != value)
                {
                    _type = value;
                    RaisePropertyChanged("Type");
                }
            }
        }

        [DataMember(Order = 3)]
        public DateTime TimeStamp { get; private set; }


        private IContact _sender;
        public IContact Sender
        {
            get { return _sender; }
            set
            {
                if (_sender != value)
                {
                    _sender = value;
                    RaisePropertyChanged("Sender");
                }
            }
        }
        //public IContact Sender
        //{
        //    get
        //    {
        //        IContact contact = null;
        //        Messenger.Default.Send(new NotificationMessageAction<IContact>(this, SenderID, "GetContactFromUserID", cont =>
        //           {
        //               contact = cont;
        //               this.FontColor = cont.FontColor.Color;
        //               //Task.Run(() =>
        //               //    {
        //               //        FontColor = StaticMethods.ColorToHexString(cont.FontColor.Color);
        //               //    });
        //           }));
        //        if (contact == null)
        //        {
        //            contact = JiveContactModel.CreatePlaceholderUser(SenderID);
        //            contact.FontColor = new SolidColorBrush(this.FontColor);
        //        }
        //        return contact;
        //    }
        //}

        private bool _isUserNameVisible;
        [DataMember(Order = 4)]
        public bool IsUserNameVisible
        {
            get { return _isUserNameVisible; }
            set
            {
                if (_isUserNameVisible != value)
                {
                    _isUserNameVisible = value;
                    RaisePropertyChanged("IsUserNameVisible");
                }
            }
        }

        private string _messageText;
        [DataMember(Order = 5)]
        public string MessageText
        {
            get
            {
                return _messageText;
            }
            set
            {
                if (value != this._messageText)
                {
                    this._messageText = value;
                    if (!_isDeserializing)
                    {
                        if (DesignMode.DesignModeEnabled)
                            MessageItems = RichTextBlockExtension.SplitUrlAndEmoticons(value, "ArbitraryDesignTimeID");
                        else
                            MessageItems = RichTextBlockExtension.SplitUrlAndEmoticons(value, this.SenderID);
                    }
                    RaisePropertyChanged("MessageText");
                }
            }
        }

        private List<MessageTextObjectMatch> _messageItems;
        public List<MessageTextObjectMatch> MessageItems
        {
            get { return _messageItems; }
            private set
            {
                if (_messageItems != value)
                {
                    _messageItems = value;
                    RaisePropertyChanged("MessageItems");
                }
            }
        }


        public MessageModel(IContact sender, DateTime timeStamp, MessageModelType type)
        {
            this.SenderID = sender.UserID;
            //    this.UserID = userID;
            //    this.UserName = UserID.Remove(userID.IndexOf("#"));
            //  this.dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
            this.Type = type;
              this.Sender = sender;
            this.TimeStamp = timeStamp;
        }
        public MessageModel()
        {
            //  this.dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
        }
        [OnDeserialized]
        internal void Initialize(StreamingContext context)
        {
            _isDeserializing = false;
        }
        [OnDeserializing]
        internal void PreInitialize(StreamingContext context)
        {
            _isDeserializing = true;
        }
        public void RefreshMessageItems()
        {
            MessageItems = RichTextBlockExtension.SplitUrlAndEmoticons(this.MessageText, this.SenderID);
        }
        //private async Task<Paragraph> CreateParagraph()
        //{
        //    return await RichTextBlockExtension.MakeParagraph(RichTextBlockExtension.SplitUrlAndEmoticons(_messageText, Sender.UserID));
        //}
        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
